<template>
    <views class="terrace_role_amend">
        <terrace-role-detail 
        :type="type" 
        :role_name="role_name"
        :status="status"
        :remark="remark"
        :permission_ids="permission_ids" 
        :default_checked_keys="default_checked_keys"
        :default_expanded_keys="default_expanded_keys"  
        @submitRole="submitRole"
        >
        </terrace-role-detail>

    </views>
</template>


<script>
    import TerraceRoleDetail from 'components/terrace/terrace_role/child_component/chile_component/terrace_role_detail';

export default {
    name: 'TerraceRoleAmend',
    data(){
        return  {
            admin_role_id:0,        //角色ID
            role_name:'',           //角色名称
            status:0,               //状态
            type:2,                 //编辑的type类型是 0
            remark:'',              //备注
            permission_ids:[],
            default_expanded_keys:[],        //默认展开tree 的 id
            default_checked_keys:[],         //默认选中的 tree 的 id
        }
    },
    components:{
        TerraceRoleDetail
    },
    mounted(){
        //根据路由来的参数 query   ： this.$route.query
        console.log("页面响应玩",this.$route)
        this.$setRouteTitle();
        this.admin_role_id = this.$route.query.admin_role_id;

        //调取接口，查看角色ID详情
        this.$raxios.post(this.$mapapi.getRoleDetail,{data:{admin_role_id:this.admin_role_id}}).then(res => {
                if(res.code != 200&&res.code!=501)    return Promise.reject(res.msg);
                this.role_name = res.data.admin_role_name;
                this.remark = res.data.remark;
                this.status = res.data.status;
                this.permission_ids = res.data.permissions;
                for(let val of res.data.permissions){
                        this.default_expanded_keys.push(val.permission_id);
                        if(parseInt(val.is_active) == 1){
                                this.default_checked_keys.push(val.permission_id);
                        }
                        if(val.list.length > 0){
                                for(let vo of val.list){
                                        this.default_expanded_keys.push(vo.permission_id);
                                        if(parseInt(vo.is_active) == 1){
                                                this.default_checked_keys.push(vo.permission_id);
                                        }
                                        for(let vi of vo.list){
                                                this.default_expanded_keys.push(vi.permission_id);
                                                if(parseInt(vi.is_active) == 1){
                                                        this.default_checked_keys.push(vi.permission_id);
                                                }
                                        }
                                }
                        }
                }
        }).catch(res => {
                this.$message.error(res);
        });
    },
    methods:{
        submitRole(data){
            console.log(data);
            data.admin_role_id = this.admin_role_id;
            this.$raxios.post(this.$mapapi.saveAdminRole,{data}).then(res =>{
                if(res.code != 200)     return  Promise.reject(res.msg);
                this.$message.success(res.msg);
                setTimeout(
                    this.$router.push('/terrace_role')
                    ,2000);
            }).catch(res => {
                console.log(res);
                this.$message.error(res);
            });

        }
    }
    
}
</script>